<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html><head> 
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  <title>CGI deployment on Apache (web.py)</title> 
  <link rel="stylesheet" type="text/css" href="/static/webpy-new.css"/> 
  <link rel="SHORTCUT ICON" href="/static/favicon.ico"/> 
</head><body> 
 
        <div id="header"><table class="header"><tr>
                <td class="logo"><a href="/" title="Home"><img src="/static/webpy.gif" alt="" /></a></td> 
<td class="blurb">&ldquo;Think about the ideal way to write a web app.<br />Write the code to make it happen.&rdquo; [<a href="/philosophy">more...</a>]</td> </tr>
</table>
        </div> 
        <div id="location"> 
                <a href="/">home</a> &gt; CGI deployment on Apache
        </div> 
<div id="container"> 
<div id="sidebar">
<strong>get started:</strong>
<ul class="body">
  <li><a href="/install">install</a></li>
  <li><a href="/docs/0.3/tutorial">tutorial</a></li>
</ul>
<strong>learn more:</strong>
<ul class="body">
<li><a href="/docs/0.3/api">api reference</a></li>
<li><a href="/cookbook/">cookbook</a></li>
<li><a href="/src/">code examples</a></li>
</ul>

<strong>dive in:</strong>
<ul class="body"> 
<li><a href="https://github.com/webpy/webpy">source code</a></li>
<li><a href="http://groups.google.com/group/webpy/">mailing list</a></li>
<li><a href="https://github.com/webpy/webpy/issues">issue tracker</a></li>
</ul>
</div>

<div id="main"> 
<div id="content" class="body"> 
<h1>CGI deployment on Apache</h1>

<p>Here are the simple steps needed to create and run an web.py application.</p>

<ul>
<li><p>Install web.py and flups</p></li>
<li><p>Create the application as documented</p>

<pre><code>  if __name__ == "__main__":
      web.run(urls, globals())
</code></pre></li>
</ul>


<p>For our example, let it be named <code>app.py</code>, located in <code>/www/app</code> and we need it accessible as <code>http://server/app/app.py</code>.</p>

<ul>
<li><p>Configure Apache (version 2.2 in this example)</p>

<pre><code>  ScriptAlias /app "/www/app/"
  &lt;Directory "/www/app/"&gt;
          Options +ExecCGI +FollowSymLinks
          Order allow,deny
          Allow from all
  &lt;/Directory&gt;
</code></pre></li>
</ul>


<p>That's it. Your application is accessible via <code>http://server/app/app.py/</code>. Additional URLs handled by the application are added to the end of the URL, for examples <code>http://server/app/app.py/myurl</code>.</p>

<ul>
<li><p>.htaccess configuration</p>

<pre><code>        Options +ExecCGI
        AddHandler cgi-script .py
        DirectoryIndex index.py
        &lt;IfModule mod_rewrite.c&gt;
            RewriteEngine on
            RewriteBase /
            RewriteCond %{REQUEST_FILENAME} !-f
            RewriteCond %{REQUEST_FILENAME} !-d
            RewriteCond %{REQUEST_URI} !^/favicon.ico$
            RewriteCond %{REQUEST_URI} !^(/.*)+index.py/
            RewriteRule ^(.*)$ index.py/$1 [PT]
        &lt;/IfModule&gt;
</code></pre></li>
</ul>


<p>Here it is assumed that your application is called index.py. The above htaccess checks if some static file/directory exists failing which it routes the data to your index.py. Change the Rewrite Base to a sub-directory if needed.</p>

<div style="clear: both;"></div> 
 
</div> 
 
</div> 
</div>
        <div id="footer"> 
    <p>started by <a style="color: gray;" href="http://www.aaronsw.com/">Aaron Swartz</a> (<a style="color: black;" href="mailto:webpy@aaronsw.com">webpy@aaronsw.com</a>)</p>

<p><a href="https://github.com/webpy/webpy.github.com/blob/master/cookbook/cgi-apache.zh-cn.md">fork this page on GitHub</a></p>
</div> 
 
</body> 
</html> 
